Public Class ActiveDirectoryForm

  Private Sub ADNameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ADNameButton.Click

    OutputTextBox.Text = String.Empty

    If InputTextBox.Text.Length > 0 Then

      Try
        OutputTextBox.Text = dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.UserContactName(InputTextBox.Text)
      Catch ex As Exception
        OutputTextBox.Text = ex.Message
      End Try

    End If


  End Sub

  Private Sub ADEmailButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ADEmailButton.Click

    OutputTextBox.Text = String.Empty

    If InputTextBox.Text.Length > 0 Then

      Try
        OutputTextBox.Text = dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.UserEmail(InputTextBox.Text)
      Catch ex As Exception
        OutputTextBox.Text = ex.Message
      End Try

    End If

  End Sub

  Private Sub ADInfoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ADInfoButton.Click

    OutputTextBox.Text = String.Empty

    If InputTextBox.Text.Length > 0 Then

      Try
        Dim info As dotNetTips.Utility.DirectoryServices.UserInfo = dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.UserInfo(InputTextBox.Text)

        Dim sb As New System.Text.StringBuilder(200)

        sb.AppendLine(info.ContactName)
        sb.AppendLine(info.Department)
        sb.AppendLine(info.Email)
        sb.AppendLine(info.Manager)
        sb.AppendLine(info.DeliveryOffice)
        sb.AppendLine(info.Telephone)
        sb.AppendLine(info.Fax)
        sb.AppendLine(info.FirstName)
        sb.AppendLine(info.LastName)
        sb.AppendLine(info.Title)

        OutputTextBox.Text = sb.ToString

      Catch ex As Exception
        OutputTextBox.Text = ex.Message
      End Try

    End If

  End Sub

  Private Sub ValidateADAccountButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ValidateADAccountButton.Click

    OutputTextBox.Text = String.Empty

    If InputTextBox.Text.Length > 0 Then

      Try
        OutputTextBox.Text = dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.ValidateAccountName(InputTextBox.Text).ToString
      Catch ex As Exception
        OutputTextBox.Text = ex.Message
      End Try

    End If

  End Sub

  Private Sub IsUserInGroupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IsUserInGroupButton.Click

    OutputTextBox.Text = String.Empty

    If InputTextBox.Text.Length > 0 Then

      Try
        OutputTextBox.Text = dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.IsUserInGroup(InputTextBox.Text).ToString
      Catch ex As Exception
        OutputTextBox.Text = ex.Message
      End Try

    End If

  End Sub

  Private Sub LoadAllUsersButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadAllUsersButton.Click
    OutputTextBox.Text = String.Empty

    Dim sb As New System.Text.StringBuilder

    For Each user As dotNetTips.Utility.DirectoryServices.UserInfo In dotNetTips.Utility.DirectoryServices.ActiveDirectoryHelper.LoadAllUsers

      sb.AppendLine(string.Format("{0} - {1} - {2}", user.ContactName, user.Email, user.DeliveryOffice))

    Next

    OutputTextBox.Text = sb.ToString

  End Sub
End Class
